<%@ page contentType="text/html; charset=UTF-8"%>
<%@ include file="/WEB-INF/view/system/common/headLogin.jsp"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<script type="text/javascript" src="asserts/plugins/easyui/treegrid-dnd.js" ></script>
<script type="text/javascript">

$(document).ready(function() {
	setTreeGridList();
	showGroupDetail('${loginedUserGroupId}');
});

var groupTypeListJSON = eval('(${groupTypeListJSON})');

var refreshGrid = function(){
	$("#groupTreegrid").treegrid('reload');
};

function setTreeGridList(){
	$('#groupTreegrid').treegrid({
		title:'用户分组树',
		striped:true,fit:true,
		nowrap:false,
		fitColumns:true,
		rownumbers:true,
		fixed:true,
		animate:true,
		url:'usergroup/getGroups.action',
		idField:'id',
		treeField:'name',
		frozenColumns:[[{title:'名称',field:'name',width:'40%',
			formatter:function(value,rowData){
				var showValue="";
				var s = $('#groupTreegrid').treegrid('getChildren',rowData.id);
				var ranks = rowData.rank; 
				for(var i = 0;i<s.length;i++){
					ranks += s[i].rank;
				}
				if(rowData.type==2){
					showValue = '<span style="color:blue">'+value+'('+ranks+')</span>';
				} else {
					showValue='<span style="color:red">'+value+'('+ranks+')</span>';
				}
				return showValue;
		}}]]
		,columns:[[{field:'code',title:'编码',width:'10%'}
			,{field:'type',title:'类型',width:'15%',formatter:function(val){
				for(var i = 0 ; i < groupTypeListJSON.length; i++){
					if(groupTypeListJSON[i].value == val){
						return groupTypeListJSON[i].name;
					}
				}
	            return val;
			}}
			,{field:'descr',title:'分组描述',width:'30%'}
		]]
		,tools: [{iconCls: 'icon-reload',handler: function(){$("#groupTreegrid").treegrid('reload');}}]
		,onContextMenu: function(e,row){
			e.preventDefault();
			$(this).treegrid('unselectAll');
			$(this).treegrid('select', row.id);
			createColumnMenu(row);
			$('#tmenu').menu('show', {
				left: e.pageX,
				top: e.pageY
			});
		}
		,onDblClickRow:function(row){
			showGroupDetail(row.id);
		}
	});
}



var tmenu;
function createColumnMenu(rowData){
	
	var childLength = $('#groupTreegrid').treegrid('getChildren',rowData.id).length;
	if(tmenu!=null){$("#tmenu").remove();}
	var menuStr='<div id="tmenu" style="width:135px;">';
    

	
		<%if (SysAuthUtil.ifBtn(request, "groupAdd")) {%>
		if (rowData.type == 0) { //新增下级分组
			menuStr += '<div data-options="iconCls:\'icon-add\'" id ="addSubGroup">添加总分组</div>';
	    } else {
			menuStr += '<div data-options="iconCls:\'icon-add\'" id ="addSubGroup">添加子分组</div>';
		}

		<%}%>
		menuStr += '<div data-options="iconCls:\'icon-edit\'" id ="update">修改</div>'; //更新分组信息

		if (childLength == 0 && rowData.type != 0) { //有子分组或者是总分组无法删除
			menuStr += '<div data-options="iconCls:\'icon-remove\'" id ="remove">删除</div>'; //删除分组
		}

		menuStr += '<div data-options="iconCls:\'icon-ok\'" id ="showGroupDetail">双击可看详情</div>'; //查询分组信息
		<%if (SysAuthUtil.ifBtn(request, "userAdd")) {%>
		menuStr += '<div data-options="iconCls:\'icon-add\'" id ="addUser">添加用户</div>'; //添加分组用户
		<%}%>
		menuStr += '</div>';

		tmenu = $(menuStr).appendTo('body');
		tmenu.menu({
			onClick : function(item) {
				if (item.id == 'addUser') {
					goAddUser(rowData);
				} else if (item.id == 'addSubGroup') {
					showAddGroup(rowData);
				} else if (item.id == 'showGroupDetail') {
					showGroupDetail(rowData.id);
				} else if (item.id == 'update') {
					showGroupUpdate(rowData);
				} else if (item.id == 'remove') {
					deleteGroup(rowData.id);
				}
			}
		});
	}
	
	var deleteGroup = function(groupId){
		$.messager.confirm("提示信息","确定要删除该分组吗?", function(r) {
					if (r) {
						$.axs("usergroup/doGroupRemove.action", {'groupId':groupId}, function(data){
							if(data.status == 1){
								$.messager.alert("提示信息",
										data.msg, 'info', function() {
									$('#groupTreegrid').treegrid('load');
								});
							}else{
								$.messager.alert("提示信息", data.msg, 'error');
							}
						});
					}
				});
	};
	

	function showAddGroup(rowData) {
		document.getElementById("groupAddFrame").src = "view/goGroupAdd.action?parentGroupId="
				+ rowData.id;
	}

	function showGroupDetail(rowId) {
		document.getElementById("groupAddFrame").src = "view/goGroupDetail.action?groupId="
				+ rowId;
	}
	
	function showGroupUpdate(rowData) {
		document.getElementById("groupAddFrame").src = "view/goGroupUpdate.action?groupId="
				+ rowData.id;
	}
	
	function goAddUser(rowData) {  //view/goUserAddWidthGroup.action?groupId=+ rowData.id
		//addTab('用户添加', 'view/goViewWithoutData/user_userAdd.action', '#tt',"parent");
		addTab('用户添加', 'view/goUserAddWidthGroup.action?groupId='+ rowData.id, '#tt',"parent");
	}
	
	
	function callOperatorBack(data) {
		if (data != "") {
			var json = eval("(" + data + ")");
			showMessage("<s:text name='mes.message'/>", json);
			if (json.records[0].messageCode == messageCodeSuccess) {
				
			}
		}
	}
</script>
<body>
	<table style="width: 100%; height: 100%">
		<tr>
			<td style="width: 50%; vertical-align: top;">
				<table id="groupTreegrid"></table>
			</td>
			<td style="width: 50%; vertical-align: top;">
				<iframe id="groupAddFrame" style="width: 100%; height: 100%;border:0px;overflow:auto;" >
				</iframe>
			</td>
		</tr>
	</table>
</body>
</html>